package com.roosterrobotics.replicant;


import com.roosterrobotics.replicant.provider.ReplicantData;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class SensorActuatorExpandableListAdapter extends BaseExpandableListAdapter {
	private Context myContext;
	private Cursor mSensors;
	private Cursor mActuators;
	
	private static final int SENSORS = 0;
	private static final int ACTUATORS = 1;
	
	private String[] arrGroupElements;
 
	public SensorActuatorExpandableListAdapter(Context context, Cursor sensorsCursor, Cursor actuatorsCursor) {
		myContext = context;
		mSensors = sensorsCursor;
		mSensors.moveToFirst();
		mActuators = actuatorsCursor;
		mSensors.moveToFirst();
		arrGroupElements = new String[2];
		arrGroupElements[SENSORS] = myContext.getResources().getString(R.string.sensors);
		arrGroupElements[ACTUATORS] = myContext.getResources().getString(R.string.actuators);
		
	}
	  
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}
	 
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}
	 
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
	    
		if (convertView == null) {
			LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.devices_child_row, null);
		}
	    
		TextView childName = (TextView) convertView.findViewById(R.id.childName);
		if (groupPosition == SENSORS) {
			mSensors.moveToPosition(childPosition);
			childName.setText(mSensors.getString(mSensors.getColumnIndex(ReplicantData.Sensors.SENSOR_NAME_COLUMN_NAME)));
		}
	//	else {
		//	mActuators.moveToPosition(childPosition);
			//childName.setText(mActuators.getString(mActuators.getColumnIndex(ReplicantData.Actuators.ACTUATOR_NAME_COLUMN_NAME)));
	//	}
	    
	   return convertView;
	  }
	 
	  @Override
	  public int getChildrenCount(int groupPosition) {
		  if (groupPosition == SENSORS) {
			  return mSensors.getCount();
		  }
		  else {
			  return mActuators.getCount();
		  }
	  }
	 
	  @Override
	  public Object getGroup(int groupPosition) {
		  return null;
	  }
	 
	  @Override
	  public int getGroupCount() {
		  return arrGroupElements.length;
	  }
	 
	  @Override
	  public long getGroupId(int groupPosition) {
		  return 0;
	  }
	 
	  @Override
	  public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
	 
		  if (convertView == null) {
			  LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			  convertView = inflater.inflate(R.layout.devices_group_row, null);
		  }
	    
		  TextView groupName = (TextView) convertView.findViewById(R.id.groupName);
		  groupName.setText(arrGroupElements[groupPosition]);
		  return convertView;
	  }
	 
	  @Override
	  public boolean hasStableIds() {
		  return false;
	  }
	 
	  @Override
	  public boolean isChildSelectable(int groupPosition, int childPosition) {
		  return true;
	  }
}


